package com.guisu.material

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.guisu.material.databinding.ActivityMainBinding

class MainActivity: AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private var mTitleBarHeight = 0
    private var mImgHeight = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        StatusBarUtil.setActivityTranslucent(this)
        // QQ 效果 1.不断监听ScrollView的滚动，判断当前滚动的位置跟头部的 ImageView比较计算背景透明度
        // 2.自定义Behavior

        // 1.刚进来背景完全透明
        binding.titleBar.background.alpha = 0

        binding.titleBar.post {
            mTitleBarHeight = binding.titleBar.measuredHeight
        }
        binding.img.post {
            mImgHeight = binding.img.measuredHeight
        }

        // 不断的监听滚动
        binding.scrollView.setOnScrollChangeListener(object : MyScrollerView.ScrollChangeListener{
            override fun onScroll(l: Int, t: Int, oldl: Int, oldt: Int) {
                // 获取图片的高度，根据当前滚动的位置，计算alpha值
                if (mImgHeight <= 0) return

                // mImgHeight - titleBar的高度
                var alpha = t / (mImgHeight.toFloat() - mTitleBarHeight)

                if (alpha <= 0) {
                    alpha = 0f
                }

                if (alpha > 1f) {
                    alpha = 1f
                }
                binding.titleBar.background.alpha = (alpha * 255).toInt()
            }
        })
    }
}